export function formatImg(url, size) {
  return `${url}?param=${size}x${size}`
}
export function getSongUrl(id) {
  return `https://music.163.com/song/media/outer/url?id=${id}.mp3`
}
export function formatTime(time, fmt) {
  let date = new Date(time);

  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
}
function padLeftZero(str) {
  return ('00' + str).substr(str.length);
}
export function formatDuration(dt) {
  return formatTime(dt, "mm:ss");
}
export function formatCount(count) {
  if (count >= 10000) {
    return Math.floor(count/10000) + "万"
  }
  return count
}


const parseExp = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/
export function formatLyric(songLyric) {
  const lyricArr = songLyric.split('\n')
  const songLyricArr = []
  let id = 0
  for (const line of lyricArr) {
    if (line) {
      const res = parseExp.exec(line)
      if (!res) continue
      const time1 = res[1] * 60 * 1000  //分
      const time2 = res[2] * 1000  //秒
      const time3 = res[3].length === 3 ? res[3] * 1 : res[3] * 10  //毫秒
      const totalTime = time1 + time2 + time3 //总毫秒
      const content = line.replace(parseExp, "").trim();
      const obj = {
        totalTime,
        content,
        id
      }
      id++
      songLyricArr.push(obj)
    }
  }
  return songLyricArr
}